package common.world.layer
{
	import common.avatar.AvatarSprite;
	import common.core.GSprite;
	import common.core.Tab;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class EntityLy extends GSprite
	{
		private var m_entityTab : Tab;
		
		public function EntityLy()
		{
			m_entityTab = new Tab;
			
			this.addEventListener(Event.ENTER_FRAME,onFrame);
		}	
		
		/**
		 * 添加实体
		 * @param id
		 * @param entity 
		 */	
		public function addEntity(id:String,entity:DisplayObject) : void
		{
			m_entityTab.add(id,entity);
			
			this.addChild(entity);
		}
		
		/**
		 * 获取实体
		 * @param id
		 * @return  
		 */		
		public function findEntity(id:String) : DisplayObject
		{
			return m_entityTab.find(id);
		}
		
		/**
		 * 删除单个实体
		 * @param id 
		 */
		public function removeEntity(id:String) : void
		{
			m_entityTab.remove(id);
			
			this.removeChild(m_entityTab.find(id));
		}
		
		/**
		 * 删除单个实体
		 * @param id 
		 */
		public function removeAll() : void
		{
			m_entityTab.removeAll();
		}
		
		private function onFrame(evt : Event) : void
		{
			update();
		}
		
		/**
		 * 时时更新 
		 */		
		public override function update() : void
		{
			sort();
			
			for each(var child:* in m_entityTab.tab)
			{
				if(child is GSprite)
				{
					child.update();
				}
			}
			
			hitTest();
		}
		
		/**
		 * 排序 
		 */		
		private function sort() : void
		{
			var sortLst : Array = []; 
			
			for each(var child:DisplayObject in m_entityTab.tab)
			{
				sortLst.push({depth:child.y,body:child});
			}
			
			sortLst.sortOn("depth",Array.NUMERIC);
			
			for(var i:int=0;i<sortLst.length;i++)
			{
				this.setChildIndex(sortLst[i]["body"],i);
			}
		}
        	
		private function sortOnBodyRange() : void
		{
			var list : Array = m_entityTab.toArray().slice(0);
			
			var sortItems : Array = [];
			
			for(var i:int=0;i<list.length;++i)
			{
				var nsi:GSprite = list[i];
				var added : Boolean = false;
				for(var j:int=0;j<sortItems.length;j++)
				{
					var si : GSprite = sortItems[j];
					
					var start : Point = new Point(nsi.bodyRange.left+nsi.x,nsi.bodyRange.top+nsi.y);
					var end : Point = new Point(si.bodyRange.right+si.x,si.bodyRange.bottom+si.y);
					
					if( start.x <= end.x &&  start.y <= end.y )
					{
						sortItems.splice(j,0,nsi);
						added = true;
						break;				
					}
				}
				
				if(!added){
					sortItems.push(nsi);
				}
			}
			
			for(var r:int = 0; r < sortItems.length; ++r)
			{
				this.setChildIndex(sortItems[r], r);
			}
		}
		
		/**
		 * 碰撞检测 
		 */		
		public function hitTest() : void
		{
			var list : Array = m_entityTab.toArray();
			
			for(var i:int=0;i<list.length;++i)
			{
				var nsi:GSprite = list[i];
				var nsiBody : Rectangle = new Rectangle(nsi.bodyRange.left+nsi.x,nsi.bodyRange.top+nsi.y,nsi.bodyRange.width,nsi.bodyRange.height);
				
				for(var j:int=0;j<list.length;j++)
				{
					var si : GSprite = list[j];
					var siBody : Rectangle  = new Rectangle(si.bodyRange.left+si.x,si.bodyRange.top+si.y,si.bodyRange.width,si.bodyRange.height);
					
					if(i != j)
					{
						if(Math.abs(si.x - nsi.x )<= (Math.abs(si.bodyRange.width + nsi.bodyRange.width) >> 1 )&&
								Math.abs(si.y - nsi.y) <= (Math.abs(si.bodyRange.height + nsi.bodyRange.height) >> 1))
						{
							{
								nsi.collision(si);
								si.collision(nsi);		
							}
						}
						else
						{
							nsi.unCollision(si);
							si.unCollision(nsi);	
						}
					}
				}
			}
		}
		
		/**
		 * 销毁 
		 */		
		public function destory() : void
		{
		   this.addEventListener(Event.ENTER_FRAME,onFrame);	
			
		   m_entityTab.destory();
		   
		   m_entityTab = null;
		}
	}
}